<template>
  <div class="updateGHender">
    <van-picker
      show-toolbar
      :columns="columns"
      :default-index="defaultIndex"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      @change="onGenderChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from "../api/user.js";
export default {
  name: "updateGender",
  data() {
    return {
      columns: ["男", "女"],
      // 数据中刚好那个是0和1与其对应
      defaultIndex: this.value
    };
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: "保存中",
        forbidclick: true
      });

      // 提交更新用户性别
      await updateUserProfile({
        gender: this.defaultIndex
      });
      this.$toast.success("保存成功");
      // 直接让父级更新不用刷新  发布input事件，更新父组件v-model绑定的数据
      this.$emit("input", this.defaultIndex);
      // 关闭弹出层
      this.$emit("close");
    },
    onGenderChange(picker, value, index) {
      this.defaultIndex = index;
    }
  },
  props: {
    value: {
      type: Number,
      required: true
    }
  }
};
</script>
<style scoped></style>
